package ch20;
/**
 * Write a method to randomly generate a set of m integers from an array of size n. 
 * Each element must have equal probability of being chosen.
 * @author Ray
 *
 */
public class ch20_3 {
	public static int rand(int low, int high){
		return low + (int)(Math.random()*(high-low+1));
	}
	/**
	 * Return a subset of inputs contains randomly m elements
	 * @param inputs
	 * @param m
	 * @return
	 */
	public static int[] getRandSet(int[] inputs,int m){
		int[] ret = new int[m];
		int[] temp = inputs.clone();
		for(int j=0;j<m;j++){
			int i = rand(j, temp.length-1);
			ret[j] = temp[i];
			temp[i] = temp[j];
		}
		return ret;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] inputs = {1,2,3,4,5,6};
		int[] sub = getRandSet(inputs, 3);
		for(int i=0;i<sub.length;i++)
			System.out.print(sub[i]+",");
	}

}
